package com.qf.proxy.test;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.ds.DSFactory;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qf.proxy.util.SmallTool;
import jdk.nashorn.internal.scripts.JO;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.qf.proxy.util.StringUtil.DelOther;

/**
 * @Author: 葛成瑞
 * @Date: 2024/12/26 15:37
 */
public class 匹配数据 {

    @SneakyThrows
    public static void main(String[] args) {

        Db use = Db.use(DSFactory.get("data_com"));
        String table = "guide_details";
        String queryUrl = "http://10.1.1.11:65213/gcr/search";
        List<Entity> query = use.query("select * from " + table + " where deal is null and reference is not null");

        SmallTool.printMess("全部数量是+"+query.size());
        query.parallelStream().forEach(entity -> {
            int id = entity.getInt("id");
//            if (id<= 3318) {
//                continue;
//            }
            String entitle = entity.getStr("title_en").replace("\"","");
            //里面包含了doi 和期刊信息
            String reference = DelOther(entity.getStr("reference"));
            String matchDb = "";
            if (StringUtils.isNotEmpty(entitle) && StringUtils.isNotEmpty(reference)) {
                Map<String,Object> map = new HashMap<>();
                map.put("pram", entitle);
                String post = HttpUtil.post(queryUrl, JSONObject.toJSONString(map));
                String matchPmid = "";
                String matchDoi = "";
                String matchut = "";
                try {
                    if (post != null) {
                        JSONObject jsonObject = JSONObject.parseObject(post);
                        JSONArray data = jsonObject.getJSONArray("data");
                        if (data != null && data.size() > 0) {
                            if (data!=null&&data.size() == 1) {
                                JSONObject jsonObject1 = data.getJSONObject(0);
                                String string = jsonObject1.getString("dataname");
                                if (string.equals("NCBI")) {
                                    matchPmid = jsonObject1.getString("dataunique");
                                    matchDoi = jsonObject1.getString("doi");
                                    matchDb = string;
                                }else if (string.equals("SCIE")){
                                    matchut = jsonObject1.getString("dataunique");
                                    matchDoi = jsonObject1.getString("doi");
                                    matchDb = string;
                                }else if (string.equals("wanFan")){
//                            matchut = jsonObject1.getString("dataunique");
                                    matchDoi = jsonObject1.getString("doi");
                                    matchDb = string;
                                }else if (string.equals("CNKI")){
//                            matchut = jsonObject1.getString("dataunique");
                                    matchDoi = jsonObject1.getString("doi");
                                    matchDb = string;
                                }else if (string.equals("zHua")){
//                            matchut = jsonObject1.getString("dataunique");
                                    matchDoi = jsonObject1.getString("doi");
                                    matchDb = string;
                                }else {
                                    SmallTool.printMessError(string);
                                }
                            }else {
                                for (int i = 0; i < data.size(); i++) {
                                    JSONObject jsonObject1 = data.getJSONObject(i);
                                    // Pediatr Crit Care Med. 2018 Sep;19(9):884-898.
                                    // Pediatr Crit Care Med. 2018-Sep;19(9):884-898. doi: 10.1097/pcc.0000000000001613
                                    String JOUR = DelOther(jsonObject1.getString("jour"));
                                    String string = jsonObject1.getString("dataname");
                                    if (JOUR.contains(reference)) {
                                        if (string.equals("NCBI")) {
                                            matchPmid = jsonObject1.getString("dataunique");
                                            matchDoi = jsonObject1.getString("doi");
                                        }else if (string.equals("SCIE")){
                                            matchut = jsonObject1.getString("dataunique");
                                            matchDoi = jsonObject1.getString("doi");
                                        }else if (string.equals("zHua")){
//                                        matchut = jsonObject1.getString("dataunique");
                                            matchDoi = jsonObject1.getString("doi");
                                        }else {
                                            SmallTool.printMessError(string);
                                        }
                                        SmallTool.printMess("多个匹配到了 直接下一步");
                                        matchDb = string;
                                        break;
                                    }

                                }
                            }
                        }

                    }
                }catch (Exception e){
                    e.printStackTrace();
                    SmallTool.printMess(id);
                }
                if (StringUtils.isNotBlank(matchPmid)||StringUtils.isNotBlank(matchDoi)||StringUtils.isNotBlank(matchut)) {
                    SmallTool.printMess("匹配到 进行更新了+match_doi"+matchDoi+"++matchut"+matchut+"++match_pmid++"+matchPmid+"++matchDb++"+matchDb);
                    entity.set("match_doi", matchDoi);
                    entity.set("match_pmid", matchPmid);
                    entity.set("match_ut", matchut);
                    entity.set("match_db", matchDb);
                    entity.set("deal", "1");
                    Entity where = new Entity();
                    where.set("id", entity.getInt("id"));
                    where.setTableName(table);
                    try {
                        use.update(entity, where);
                    } catch (SQLException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        });

    }

}
